<?php 
class Comment extends Model{
/*
 * id
user_id
video_id
content

 */
	private $table="xdf_comment";
	//添加评论记录
	public function addComment($user_id,$video_id,$content) {
		$sql="INSERT INTO `$this->table` (`user_id`,`video_id`,`content`,`like_num`,`create_time`) value('$user_id','$video_id','$content','0',NOW());";

		return $this->_db->insert($sql);
	}
	//检查是否已经给某个评论点过赞
	public function islikeComment($comment_id,$user_id){
		$sql="SELECT * FROM `xdf_comment_favor` WHERE `user_id`='$user_id' AND `comment_id`='$comment_id';";

		return $this->_db->fetchRow($sql);
	}
	//对某个评论点赞
	public function likeComment($comment_id,$user_id){
		$sql="INSERT INTO `xdf_comment_favor`(`comment_id`,`user_id`,`create_time`) VALUES ('$comment_id','$user_id',now());";
		$updateCommentFavorNumSql="UPDATE `xdf_comment` SET like_num=like_num+1 WHERE `id`=$comment_id;";
		if($this->update($updateCommentFavorNumSql))
			return $this->_db->insert($sql);
		else 
			return false;
	}
	public function delLikeComment($comment_id,$user_id){
		$sql=" DELETE FROM `xdf_comment_favor` WHERE `comment_id`='$comment_id' AND `user_id`='$user_id';";
		$updateCommentFavorNumSql="UPDATE `xdf_comment` SET like_num=like_num-1 WHERE `id`=$comment_id;";
		if($this->update($updateCommentFavorNumSql))
			return $this->del($sql);
		else 
			return false;
	}
	//某用户获取id为video_id的点赞数最多的7个评论
	public function getBestComment($video_id){
/*	*/	$sql=" select
				a.`id` as comment_id,
				a.`content` as content,
				a.`create_time` as create_time,
				a.`like_num` as like_num,
				a.`user_id` as user_id,
				b.`name` as user_name,
				IF(c.user_id,1,0) as mylike
				FROM `{$this->table}` a
				LEFT JOIN `xdf_user` b ON b.id=a.user_id 
				LEFT JOIN `xdf_comment_favor` c on c.comment_id=a.id 
				WHERE a.`video_id` = '$video_id' ORDER BY a.`like_num` DESC LIMIT 0,7";
// 	var_dump($sql);
		return $this->fetchAll( $sql );
	
	}
	public function getCommentList($video_id,$timestamp,$pagesize,$action){
		
		//选出点赞数最多的前5个评论
		$sql="select `id` FROM `xdf_comment`	WHERE `video_id` = '$video_id' ORDER BY `like_num` DESC LIMIT 0,5;";
// 		var_dump($sql);
		$bestComment_id_result=$this->fetchAll($sql);
		$bestComment_ids=array();
		foreach($bestComment_id_result as $key =>$val){
			$bestComment_ids[]=$val["id"];
		}
		$comment_id_fliter="NOT IN (".join(",",$bestComment_ids).")";
// 		if($action=="pre")
		$preOrMore_sql="";
		if($action=='more'){//获取更多
			$preOrMore_sql="AND a.id < $timestamp";
		}
		$sql=" select
		a.`id` as comment_id,
		a.`content` as content,
		a.`create_time` as create_time,
		a.`like_num` as like_num,
		a.`user_id` as user_id,
		b.`name` as user_name,
		IF(c.user_id,1,0) as mylike
		FROM `{$this->table}` a
		LEFT JOIN `xdf_user` b ON b.id=a.user_id
		LEFT JOIN `xdf_comment_favor` c on c.comment_id=a.id
		WHERE a.`video_id` = '$video_id' ".$preOrMore_sql." AND a.id $comment_id_fliter 
			ORDER BY a.`id` DESC LIMIT 0,$pagesize";
// 		var_dump($sql);
		return  $this->fetchAll( $sql );
		
	}
}
?>
